package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.RdProjectListAdapter
import com.hinnotek.amoeba.bean.old.AmibaProjectListBean
import com.hinnotek.amoeba.bean.old.RDProjectListBean
import com.hinnotek.amoeba.databinding.IncludeRecyclerviewTwoButtonBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.ext.init
import com.hinnotek.amoeba.utilities.IntentUtils
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.StatusBarUtil
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.PublicInterfaceViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class RDProjectListActivity : BaseActivity<IncludeRecyclerviewTwoButtonBinding>() {
    private var mAdapter: RdProjectListAdapter? = null
    private var rdProjectListBean: MutableList<RDProjectListBean> = mutableListOf()
    private val viewModel by viewModels<PublicInterfaceViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.include_recyclerview_two_button)
        setLeftVisible(true)
        setTitle("研发项目")

    }

    override fun initVew() {

        StatusBarUtil.setStatusBarColor(this, R.color.colorbtns)
        bindingView.recyclerView.init()
        bindingView.recyclerView.adapter = RdProjectListAdapter().also { mAdapter = it }
        findViewById<View>(R.id.rb_del).setOnClickListener { v: View? ->
            val list: MutableList<Int> = ArrayList()
            for (rd in rdProjectListBean) {
                if (rd.select == 1) {
                    list.add(rd.amibaProjectId)
                }
            }
            ToastUtils.show(mContext, list.size.toString() + "")
            initDialog()
        }
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as RDProjectListBean
            val bundle = Bundle()
            bundle.putString("title", item.projectName)
            bundle.putInt("amibaProjectId", item.amibaProjectId)
            IntentUtils.startActivity(mContext, RDProjectDetailActivity::class.java, bundle)
        }
        findViewById<View>(R.id.rb_add).setOnClickListener { v: View? ->
            IntentUtils.startActivity(
                mContext,
                RDCreateProjectActivity::class.java
            )
        }
    }

    private fun initDialog() {
        val normalDialog = NormalDialog(mContext)
        normalDialog.content("是否确定删除？")
            .btnNum(2)
            .style(NormalDialog.STYLE_TWO)
            .isTitleShow(false)
            .contentTextSize(16.0f)
            .contentTextColor(getColor21(R.color.minute_blue))
            .show()
        normalDialog.setOnBtnClickL(object : OnBtnClickL {
            override fun onBtnClick() {
                normalDialog.dismiss()
            }
        }, object : OnBtnClickL {
            override fun onBtnClick() {
                normalDialog.dismiss()
            }
        })

    }

    override fun onResume() {
        super.onResume()
        getAmibaProjectListByOrgId()
    }

    override fun setData() {

    }

    /**
     * app端查询阿米巴下的全部研发项目接口
     */
    private fun getAmibaProjectListByOrgId() {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        val orgId = SharedPreUtils.getInteger("orgId", 0)
        viewModel.getAmibaProjectListByOrgId(orgId)
            .observe(this, object : ObserverState<MutableList<AmibaProjectListBean>>() {
                override fun onDataChange(data: MutableList<AmibaProjectListBean>) {
                    customDialog.dismiss()
                    mAdapter?.setList(data)
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })

    }


}